package com.isczy.acl.controller;

import com.alibaba.fastjson.JSONObject;
import com.isczy.acl.service.IndexService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/acl/index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    /***
     * ***************************************
     * 功能描述:根据token获取用户信息
     * @param
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:09】
     * @author modify:
     */
    @GetMapping("info")
    public DataResult info(){
        try{
            //获取当前登录用户用户名
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            Map<String, Object> userInfo = indexService.getUserInfo(username);
            return DataResult.builder().data(userInfo).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("根据token获取用户信息失败")
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:获取菜单
     * @param
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:10】
     * @author modify:
     */
    @GetMapping("menu")
    public DataResult getMenu(){
        try{
            //获取当前登录用户用户名
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            List<JSONObject> permissionList = indexService.getMenu(username);
            return DataResult.builder()
                    .data("permissionList", permissionList)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("获取菜单失败")
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述: 退出登录
     * @param
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-26 14:11】
     * @author modify:
     */
    @PostMapping("logout")
    public DataResult logout(){
        return DataResult.builder().successBuild();
    }

}
